import java.util.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * Creates an Event object used by the Calendar class
 * @author         Hwang, Garry
 * @date           August 13, 2014
 * @bugs           None
 */
public class Event {
	//Variables that are implemented within the actual file
	private String DTSTART;
	private String DTEND;
	public int PRIORITY;
	private String DESCRIPTION;
	private String SUMMARY;
	private String LOCATION;
	private String CLASSIFICATION;
	private boolean recur;
	private ArrayList<String> recurDays;
	private String recurStop;
	private String recurFreq;
	//used to convert all the date into string which is written into the file
	private StringBuilder event;
	SimpleDateFormat time = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
	SimpleDateFormat recurTime = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
	
	/**
	 * Event Constructor
	 * @param startTime start time and date
	 * @param endTime end time and date
	 * @param importance priority if time and date coincide
	 * @param descript description
	 * @param name of event
	 * @param place of event
	 * @param privacy public, private, or confidential
	 * @throws ParseException
	 */
	public Event(String startTime,String endTime, int importance, String descript, String name, String place, String privacy) throws ParseException {
		if(startTime.compareTo(endTime) > 0) {
			System.out.println("StartTime cannot be greater than endTime");
		}
		else if(importance < 0 && importance >9) {
			System.out.println("priority must be between 0 and 9");
		}
		else if(!privacy.equals("PRIVATE") && !privacy.equals("PUBLIC") && !privacy.equals("CONFIDENTIAL")) {
			System.out.println("values must be PRIVATE, PUBLIC, OR CONFIDENTIAL");
		}
		else {
			Date firstTime = time.parse(startTime);
			Date lastTime = time.parse(endTime);
			DTSTART = time.format(firstTime);
			DTEND = time.format(lastTime);
			PRIORITY = importance;
			DESCRIPTION = descript;
			SUMMARY = name;
			LOCATION = place;
			CLASSIFICATION = privacy.toUpperCase();
			event = new StringBuilder();
		}
		
	}
	/**
	 * creates a recurring event and takes the parameters and rules of recurrence such as the stop date, days of the week, and frequency
	 * @param sun sunday
	 * @param mon monday
	 * @param tues tuesday
	 * @param wed wednesday
	 * @param thurs thursday
	 * @param fri friday
	 * @param sat saturday
	 * @param stopDate date and time to stop recurrence
	 * @param freq daily, weekly, monthly, or yearly
	 * @throws ParseException
	 */
	public Event(String startTime,String endTime, int importance, String descript, String name, String place, String privacy, boolean sun, boolean mon, boolean tues, boolean wed, boolean thurs, boolean fri, boolean sat, String stopDate, String freq) throws ParseException {
		if(startTime.compareTo(endTime) > 0) {
			System.out.println("StartTime cannot be greater than endTime");
		}
		else if(importance < 0 && importance >9) {
			System.out.println("priority must be between 0 and 9");
		}
		else if(!privacy.equals("PRIVATE") && !privacy.equals("PUBLIC") && !privacy.equals("CONFIDENTIAL")) {
			System.out.println("values must be PRIVATE, PUBLIC, OR CONFIDENTIAL");
		}
		else {
			Date firstTime = time.parse(startTime);
			Date lastTime = time.parse(endTime);
			DTSTART = time.format(firstTime);
			DTEND = time.format(lastTime);
			PRIORITY = importance;
			DESCRIPTION = descript;
			SUMMARY = name;
			LOCATION = place;
			CLASSIFICATION = privacy.toUpperCase();
			event = new StringBuilder();
			recurDays = new ArrayList<String>();
			if(sun == true) {
				recurDays.add("SU");
			}
			if(mon == true) {
				recurDays.add("MO");
			}
			if(tues == true) {
				recurDays.add("TU");
			}
			if(wed == true) {
				recurDays.add("WE");
			}
			if(thurs == true) {
				recurDays.add("TH");
			}
			if(fri == true) {
				recurDays.add("FR");
			}
			if(sat == true) {
				recurDays.add("SA");
			}
			Date end = recurTime.parse(stopDate);
			recurStop = recurTime.format(end);
			if(freq.equals("DAILY") || freq.equals("WEEKLY") || freq.equals("MONTHLY") || freq.equals("YEARLY")) {
				recurFreq = freq;
			}
			else {
				System.out.println("Error: frequence must be DAILY. WEEKLY, MONTHLY, or YEARLY");
			}
			recur = true;
		}
	}
   
  /**
	 * gets the basic info about the event
	 * @return name, start time, and endtime of event
	 */
	public String getEvent() {
		StringBuilder event = new StringBuilder();
		event.append("Event name: " + SUMMARY);
		event.append(System.getProperty("line.separator"));
		event.append("From: " + DTSTART);
		event.append(System.getProperty("line.separator"));
		event.append("To: "+ DTEND);
		event.append(System.getProperty("line.separator"));
		event.append(System.getProperty("line.separator"));
		
		return event.toString();
	}
	/**
	 * builds the event into a String
	 * @return a String
	 * @throws error messages
	 */
	public String buildEvent() throws NullPointerException {
		if(event == null) {
			throw new NullPointerException("Bad Date/time OR priority value OR privacy value");
		}
		event.append("BEGIN:VEVENT");
		event.append(System.getProperty("line.separator"));
		event.append("CLASS:" + CLASSIFICATION);
		event.append(System.getProperty("line.separator"));
		event.append("DTSTART:" + DTSTART);
		event.append(System.getProperty("line.separator"));
		event.append("DTEND:" + DTEND);
		event.append(System.getProperty("line.separator"));
		if(recur == true) {
			event.append("RRULE:FREQ=" + recurFreq + ";UNTIL=" + recurStop + ";BYDAY=");
			for(int i = 0; i<recurDays.size(); i++) {
				if(i < recurDays.size() - 1) {
					event.append(recurDays.get(i) +",");
				}
				else {
					event.append(recurDays.get(i));
				}
			}
			event.append(System.getProperty("line.separator"));
		}
		event.append("DESCRIPTION:" + DESCRIPTION);
		event.append(System.getProperty("line.separator"));
		event.append("LOCATION:" + LOCATION);
		event.append(System.getProperty("line.separator"));
		event.append("PRIORITY:" + PRIORITY);
		event.append(System.getProperty("line.separator"));
		event.append("SUMMARY:" + SUMMARY);
		event.append(System.getProperty("line.separator"));
		event.append("END:VEVENT");
		event.append(System.getProperty("line.separator"));
		
		return event.toString();
	}
}
